{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "27592375",
   "metadata": {},
   "source": [
    "# 本周学习内容"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0a54c55",
   "metadata": {},
   "source": [
    "\n",
    ">1.函数的定义\n",
    ">2.函数的使用\n",
    ">3.函数的参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "48a1602b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 \n"
     ]
    }
   ],
   "source": [
    "a, b = 0, 1\n",
    "while a < 10:\n",
    "    print(a,end=\" \")\n",
    "    a, b = b, a+b\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1bf3e1d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#.....我想重复使用某一个功能（避免重复造轮子）\n",
    "#.....使用的人可以不懂代码，只要会调用函数即可 function_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83baff80",
   "metadata": {},
   "outputs": [],
   "source": [
    ">1.定义函数的一般方法\n",
    "...\n",
    "def function_name():# function_name 遵循变量定义的规则   区别数学函数==> f(x) python的函数可以自定义名称\n",
    "    '''函数的注释'''#2.函数的注释可以和使用者交流\n",
    "    函数体。        #3.函数体要和定义函数保持一定的tab缩进\n",
    "..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "92540340",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):\n",
    "    '''print a Fibonacci series up to n'''\n",
    "    a, b = 0, 1\n",
    "    while a < n:\n",
    "        print(a,end=\" \")\n",
    "        a, b = b, a+b\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5a011c56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 \n"
     ]
    }
   ],
   "source": [
    "fib(1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c3bc94ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "def holle():\n",
    "    '''打印你好，世界'''\n",
    "    print('holle world！')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c82bf755",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "holle world！\n"
     ]
    }
   ],
   "source": [
    "holle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a09d238b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello_user(username):\n",
    "    '''打印，你好，xxx同学'''\n",
    "    print('你好,'+ username.title()+'同学')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "dde6d549",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好,Lst同学\n"
     ]
    }
   ],
   "source": [
    "hello_user('lst')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79685358",
   "metadata": {},
   "source": [
    "# 3.函数的参数\n",
    ">1.位置参数 \n",
    ">>1.形式参数：定义函数时，写在function_name 括号里面的参数\n",
    ">>1.在定义函数时，形式参数不占用空间\n",
    ">2.实际参数：调用函数，可以依次指定函数名以及括号括起来的必要信息。在第五行代码中，因为此函数hello()括号中不需要任何信息，只需输入hello（）即可。和预期效果一样，打印hello，world！\n",
    ">>2.在使用函数时形式参数被分配空间，变为实际参数\n",
    ">3.默认参数\n",
    ">>1.在形式参数中，提前预先设置一个用户可能使用的参数值\n",
    ">>2.用户在使用/调用函数时，可以省略不给默认参数赋值\n",
    ">>3.用户默认参数赋值，将替换默认参数的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5107a825",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(animal_type,pet_name):\n",
    "    '''显示宠物信息'''\n",
    "    print('\\nI have a ' + animal_type + '.')\n",
    "    print('My ' + animal_type  + ' is name is '+pet_name.title() + '.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "ff4d9ff0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog is name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "#在使用describe_pet() 函数时 需要考虑 参数的位置，从而进行参数数据的输入(实参的调用)\n",
    "describe_pet('dog','豆豆')#要根据 形式参数的位置先后顺序。先写animal_type,再写pet_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "04c640a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog is name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "#关键字参数使用\n",
    "describe_pet(pet_name='豆豆',animal_type='dog')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2d5adba6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(animal_type,pet_name='dog'):\n",
    "    '''显示宠物信息\n",
    "    \n",
    "    通过打印的方式显示宠物类型和宠物名称\n",
    "    参数显示：\n",
    "    pet_name：是一个字符串\n",
    "    \n",
    "    '''\n",
    "    print('\\nI have a ' + animal_type + '.')\n",
    "    print('My ' + animal_type  + ' is name is '+pet_name.title() + '.')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9373b8ae",
   "metadata": {},
   "source": [
    "-------\n",
    "参数使用过程中的思考与问题\n",
    ">1.思考：关键字参数 和 位置参数 是否可以一起使用？\n",
    ">>1.positional argument follows keyword argument(位置参数均要放在关键字参数前面）\n",
    ">>2.test1() got multiple vaules\n",
    ">2.定义函数\n",
    "----\n",
    "*以下为测试代码：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "ec4e88d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#案例一\n",
    "def test(x,y,z):\n",
    "    '''打印下x，y,z的值'''\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "8ea1fd9e",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character in identifier (<ipython-input-35-b39212115f64>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-35-b39212115f64>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    test1(x=2，3，z=4)\u001b[0m\n\u001b[0m                ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid character in identifier\n"
     ]
    }
   ],
   "source": [
    "test1(x=2，3，z=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "431c3bb7",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'test1' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-36-091dca689907>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtest1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mz\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'test1' is not defined"
     ]
    }
   ],
   "source": [
    "test1(2,y=3,z=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "dca43429",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'test1' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-37-abf767fdec53>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtest1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'test1' is not defined"
     ]
    }
   ],
   "source": [
    "test1(2,x=3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1069de4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#案例2：\n",
    "def test1(x,z,y):"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7788b7e3",
   "metadata": {},
   "source": [
    "# 4.课本实践\n",
    ">1.请大家总结函数的优势\n",
    ">2，预习课本第四章"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "1f1d5818",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    '''判断输入文本出现的元音字母有哪些'''\n",
    "    vowels = set('aeiou')\n",
    "    word = set(word)\n",
    "    print(vowels & word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "2425cb01",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'i', 'o', 'u'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels('wqyqiosu')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa89fc6f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
